package xyz.showbooks.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.dfm.util.JsonResult;
import xyz.dfm.util.WebUtil;
import xyz.dfm.vo.PageResult;
import xyz.dfm.pojo.Permission;
import xyz.dfm.pojo.Role;
import xyz.showbooks.service.PermissionService;


import java.util.List;

/**
 * 此类提供用户权限相关接口
 */
@CrossOrigin
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取指定用户的权限
     * @param roleId 用户id
     * @return 用户拥有的权限
     */
    @GetMapping("/findPermission/{roleId}")
    public List<String> findPermissionById(@PathVariable Integer roleId){
        return permissionService.findPermissionById(roleId);
    }

    //根据用户Id查找用户角色及权限（后台版本）
    @GetMapping("/findRoleById/{userId}")
    public JsonResult<Role> findRoleById(PageResult pageResult){
        JsonResult jsonResult=new JsonResult();
        if(WebUtil.intIsNull(pageResult.getQuery())){
            return jsonResult.code(201).msg("参数错误");
        }return jsonResult.code(200).data(permissionService.findRoleById(pageResult));
    }

    /**
     * 根据用户id查询其所拥有所有权限（网关版本）
     * @param userId
     * @return
     */
    @GetMapping("/selectUserPermission/{userId}")
    public List<String> selectUserPermission(@PathVariable Integer userId){
        if(userId<0){
            return null;
        }
        List<String> permissions = permissionService.selectUserPermission(userId);
        return permissions;
    }
}
